package com.gonsin.ta.logic.node.groovy;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gonsin.ta.logic.TA;
import com.gonsin.ta.logic.enums.NodeTypeEnum;
import com.gonsin.ta.logic.node.Node;
import com.gonsin.ta.logic.parse.condition.context.ChainContextImpl;
import com.gonsin.ta.logic.parse.serialize.SerializeContext;
import com.gonsin.ta.logic.parse.serialize.TaDeserializable;
import com.google.gson.annotations.Expose;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;

/**
 * groovy 脚本节点
 */
@Slf4j
public class GroovyNodeAdapter extends Node {

    @JsonIgnore
    @Expose(serialize = false, deserialize = false)
    private transient String groovyScript;

    private NodeTypeEnum nodeType;

    @JsonIgnore
    @Expose(serialize = false, deserialize = false)
    private transient GroovyScriptExecutor scriptExecutor;

    // groovy可执行实例

    @JsonIgnore
    @Expose(serialize = false, deserialize = false)
    private transient Object groovyInstance;

    @JsonIgnore
    @Expose(serialize = false, deserialize = false)
    private transient Class nodeClass;

    private String nodeId;

    /**
     * 文件名字
     */
    private String filename;

    public GroovyNodeAdapter(String nodeId, NodeTypeEnum nodeType, String groovyScript, String filename, GroovyScriptExecutor groovyScriptExecutor) {
        super(nodeId);
        this.nodeId = nodeId;
        this.nodeType = nodeType;
        this.groovyScript = groovyScript;
        this.scriptExecutor = groovyScriptExecutor;
        this.filename = filename;
        this.groovyInstance = this.scriptExecutor.parse(this, groovyScript);
    }

    public GroovyNodeAdapter(String nodeId, NodeTypeEnum nodeType, Class nodeClass, String groovyScript, String filename, GroovyScriptExecutor groovyScriptExecutor) {
        super(nodeId);
        this.nodeId = nodeId;
        this.nodeType = nodeType;
        this.nodeClass = nodeClass;
        this.groovyScript = groovyScript;
        this.scriptExecutor = groovyScriptExecutor;
        this.filename = filename;
        this.groovyInstance = this.scriptExecutor.parseClass(this, nodeClass);
    }

    @Override
    public NodeTypeEnum getNodeType() {
        return nodeType;
    }

    @Override
    public void execute(ChainContextImpl context) throws Exception {
        long now = System.currentTimeMillis();
        scriptExecutor.execute(groovyInstance, context);
        if(TA.LOG){
            long delay = System.currentTimeMillis() - now;
            TA.log().info("TA - 执行【{}】耗时【{}s】", getNodeId(), new SimpleDateFormat("ss.SSSS").format(delay));
        }
//        scriptExecutor.execute(compiledScript, context);
    }

    public String getFilename() {
        return filename;
    }

    /**
     * 可能编译之后才会知道node的类型，所以在编译完之后设置该node的类型
     */
    protected void setNodeType(NodeTypeEnum nodeType) {
        this.nodeType = nodeType;
    }


    /**
     * 可能编译之后才会知道node的类型，所以在编译完之后设置该node的类型
     */
    protected void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    @Override
    public String getNodeId() {
        return nodeId;
    }

    public String getGroovyScript() {
        return groovyScript;
    }
}
